package utils;



import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.regex.Pattern;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;



/**
 * 
 *
 * @author xlouwette
 * @version $Revision:$ $Date:$ $Author:$
 */

public class SRTUtils {

	public static final String CONFIGURATION = "C:\\Tools\\configuration.properties";
    private static final String CARRIAGE_RETURN_SYSTEM = "line.separator";
    private static final int MIN_CARRIAGE_RETURN = 1;
    private static Map<Severity,Integer> map = new HashMap<Severity,Integer>();
    public static final String EMPTY_STRING = "";
    
    static{
        map.put(Severity.ERROR, JOptionPane.ERROR_MESSAGE);
        map.put(Severity.WARNING, JOptionPane.WARNING_MESSAGE);
        map.put(Severity.INFORMATION, JOptionPane.INFORMATION_MESSAGE);
        map.put(Severity.PLAIN, JOptionPane.PLAIN_MESSAGE);
    }
    
    
    /**
     * Return the extension of the file
     * @param f File
     * @return the extension of the file
     */
    public static String getExtensionOfFile(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }
    
    
    /**
     * Add zero before the time(Hours,Minutes,Seconds,Milliseconds)
     * to have a format of date like that 00:00:24,000 instead of 0:0:24,0
     * @param time the time
     * @param sizeExpected the size of the time that we will.
     * @return the good size for the time
     */
    public static String addZeroBeforeTime(int time,int sizeExpected){
        String timeInString = EMPTY_STRING+time;
        return StringUtils.leftPad(timeInString, sizeExpected,"0");
    }
    
    /**
     * Convert Calendar to a string format like that 00:00:24,000
     * @param calendar the calendar
     * @return a string Date
     */
    public static String convertCalendarToString(Calendar calendar){
        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        int minutes = calendar.get(Calendar.MINUTE);
        int seconds = calendar.get(Calendar.SECOND);
        int milliseconds = calendar.get(Calendar.MILLISECOND);
        return addZeroBeforeTime(hours,2)+":"+addZeroBeforeTime(minutes,2)+
        ":"+addZeroBeforeTime(seconds,2)+","+addZeroBeforeTime(milliseconds,3);
    }
    
    /**
     * Return one carriage return 
     * which is specific for each operating system.
     * @return the string code for the carriage return
     */
    public static String getOneCarriageReturn(){
        return getCarriageReturn(1);
    }

    /**
     * Return several carriage return
     * @param number number (much be equals or greater than one)
     * @return several string code for the carriage return
     */
    public static String getCarriageReturn(int number){
        String carriageReturn = EMPTY_STRING;
        if(number<MIN_CARRIAGE_RETURN){
            throw new NumberFormatException();
        }
        for(int i=0;i<number;i++){
            carriageReturn = carriageReturn + System.getProperty(CARRIAGE_RETURN_SYSTEM);
        }
        return carriageReturn;
    }
    
    public static void clear(JTextField textField){
        textField.setText(null);
    }
    
    public static int getTotalTimeInMilliSeconds(String minutes,String seconds,String milliseconds){
        return (Integer.parseInt(minutes)*60000)+(Integer.parseInt(seconds)*1000)+(Integer.parseInt(milliseconds));
    }
    
    public static int getTotalTimeInMilliSeconds(Calendar calendar){
    	return (calendar.get(Calendar.MINUTE)*60000)+(calendar.get(Calendar.SECOND)*1000)+(calendar.get(Calendar.MILLISECOND));
    }
    
    public static boolean isNumOfSub(String line){
        return StringUtils.isNumeric(line)&& StringUtils.isNotBlank(line);
    }

    public static boolean isTimeOfSub(String line){
        String pattern = "[0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9][0-9][0-9] --> [0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9][0-9][0-9]";
        return Pattern.matches(pattern, line);
    }

    public static boolean isTranslation(String line){
        return !isNumOfSub(line) && !isTimeOfSub(line) && StringUtils.isNotBlank(line);
    }
    
    public static String getTranslation(String translation){
        Properties properties = new Properties();
        Locale locale;
        ResourceBundle messages;
        try {
            properties.load(new FileInputStream("C:\\Tools\\configuration.properties"));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String value = properties.getProperty("locale");
        if(value!=null){
            locale = new Locale(value);
        }else{
            locale = Locale.getDefault();
        }
        messages = ResourceBundle.getBundle("language.Translation",locale);
        return messages.getString(translation);
    }
    
    public enum Severity{
        ERROR(),INFORMATION(),PLAIN(),WARNING()
    }
    
    public static int getSeverity(Severity severity){
        return getSeverity(severity);
    }
    
    
}
